﻿using Asp.Versioning;
using FlyingEye.DeviceMaintenanceServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


namespace FlyingEye.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/device-ops-service/v{version:apiVersion}/device-part-check-items")]
    [ControllerName("DevicePartCheckItem")]
    [Authorize]
    public class DevicePartCheckItemController : DefaultController
    {
        private readonly DevicePartCheckItemService _devicePartCheckItemService;

        public DevicePartCheckItemController(DevicePartCheckItemService devicePartCheckItemService)
        {
            _devicePartCheckItemService = devicePartCheckItemService;
        }

        [HttpGet("{id}", Name = "GetByCheckItemId")]
        public async Task<ActionResult<DevicePartCheckItemResponse>> GetAsync(Guid id)
        {
            return await GetInvokeAsync(async () =>
            {
                return await _devicePartCheckItemService.GetAsync(id);
            });
        }

        [HttpPost]
        public async Task<ActionResult<DevicePartCheckItemResponse>> InsertAsync([FromBody] CreateDevicePartCheckItemRequest checkItem)
        {
            return await InsertInvokeAsync(async () =>
            {
                return await _devicePartCheckItemService.InsertAsync(checkItem);
            }, "GetByCheckItemId", (response) => new { id = response.Id });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await DeleteInvokeAsync(async () =>
            {
                await _devicePartCheckItemService.DeleteAsync(id);
            });
        }
    }
}
